import RescacheMgr from "../inc/rescachemgr";
import Button from "../gui/button";
import Image from "../gui/image";
import Layer from "../gui/layer";

var d = {};

/**
 * 判断字符串是否为#开头，如果是则返回去除#后的字符串，如果否则返回false
 * @param str
 * @returns {Boolean|String}
 */


// const THREE = window.THREE;
export default class DisplayEx {
    static init() {
        DisplayEx._constant();
        DisplayEx._short();
        DisplayEx._extendObject3D();
        DisplayEx._extendSprite();
    }

    static _constant() {
        d.w = window.innerWidth;
        d.h = window.innerHeight;
    }

    static _short() {

        d.button = function (pathokey) {
            return new Button(pathokey);
        }

        d.sp = function (pathokey) {
            return new Image(pathokey);
        }

        d.layer = function(color, width, height){
            return new Layer(color, width, height);
        }

        d.ground = function (tp, width, height) {
            width = width || 1;
            height = height || 1;
            let loader = new THREE.TextureLoader();
            let groundTexture = loader.load(tp);
            groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
            groundTexture.repeat.set(25, 25);
            groundTexture.anisotropy = 16;

            let groundMaterial = new THREE.MeshLambertMaterial({ map: groundTexture });

            let mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(width, height), groundMaterial);
            mesh.position.y = 0;
            mesh.rotation.x = - Math.PI / 2;
            mesh.receiveShadow = true;
            return mesh;
        }

        d.skybox = function (tplsit) {
            let envMap = new THREE.CubeTextureLoader()
                .load(tplsit);
            envMap.format = THREE.RGBFormat;
            return envMap;
        }
    }

    static _extendObject3D() {
        let _p = THREE.Object3D.prototype;

        _p.removeFromParent = function () {
            if (this.parent) {
                this.parent.remove(this);
            }
        }

    }

    static _extendSprite() {
        let _p = THREE.Sprite.prototype;

        _p.pos = function (x, y) {
            this.position.x = x;
            this.position.y = y;
            return this;
        }
    }
}
window.d = d;